package com.spice.service.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spice.common.exception.SpiceException;
import com.spice.common.result.Response;
import com.spice.common.result.ResultCodeEnum;
import com.spice.common.util.CheckUtil;
import com.spice.service.entity.pojo.SourceCheck;
import com.spice.service.mapper.SourceCheckMapper;
import com.spice.service.service.SourceCheckSercive;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

@Service
public class SourceCheckServiceImpl extends ServiceImpl<SourceCheckMapper,SourceCheck> implements SourceCheckSercive {

    @Override
    public Response saveSourceCheck(SourceCheck newSourceCheck) {
    //确认必要参数
        if (CheckUtil.objectsHasNull(
                newSourceCheck.getCheckTime(),
                newSourceCheck.getDemoname(),
                newSourceCheck.getSender()
        ))
        {return Response.setResult(ResultCodeEnum.PARAM_LACK);}
        if (this.save(newSourceCheck)) {
            return Response.ok().message("添加原料批次入场检验数据成功").data("data",newSourceCheck);
        }
        else {
            return Response.error().message("添加原料批次入场检验数据失败");
        }
    }

    @Override
    public Response removeSourceCheck(Long id) {
        // 确认必要参数
        if (CheckUtil.objectsHasNull(id)) {
            throw new SpiceException(ResultCodeEnum.PARAM_LACK);
        }
        //确认要删除的是否存在
        SourceCheck removedSourceCheck = this.getById(id);
        if (CheckUtil.objectsHasNull(removedSourceCheck)) {
            throw new SpiceException().message("该原料批次入场检验数据不存在");
        }
        this.removeById(id);
        return Response.ok().message("原料批次入场检验数据删除成功").data("data",removedSourceCheck);
    }

    @Override
    public Response updateSoueceCheck(Long id, SourceCheck newSourceCheck) {
        //确认必要参数
        if (CheckUtil.objectsHasNull(id, newSourceCheck)) {
            return Response.setResult(ResultCodeEnum.PARAM_LACK);
        }
        //确认待更新数据是否存在
        SourceCheck updatedSourceCheck = this.getById(id);
        if (CheckUtil.objectsHasNull(updatedSourceCheck))
        {
            throw new SpiceException().message("该原料批次入场检验数据不存在");
        }
        //更新待更新的数据id和createTime
        newSourceCheck.setId(id);
        newSourceCheck.setCreateTime(updatedSourceCheck.getCreateTime());
        if (this.updateById(newSourceCheck)) {
            return Response.ok().message("原料批次入场检验数据更新成功").data("data",newSourceCheck);
        }
        else {
            return Response.error().message("原料批次入场检验数据更新失败");
        }
    }
}
